package com.mall.common.response;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.ReUtil;
import com.mall.common.constant.ResponseConstant;
import org.apache.commons.lang3.StringUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 网关响应实体
 * @author: xrj
 * @date: 2022/4/27 22:14
 * @description:
 */
public class Response {
    public static Map<String, Object> success() {
        return success(null);
    }

    public static Map<String, Object> success(String msg) {

        if (StringUtils.isNotBlank(msg) && ReUtil.contains(PatternPool.CHINESE, msg)) {
            Map<String, Object> response = new LinkedHashMap<>(2);
            response.put("success",ResponseConstant.TRUE);
            response.put("code", ResponseConstant.SUCCESS);
            response.put("message", msg);
            return response;
        } else {
            return success(msg, null);
        }
    }

    public static Map<String, Object> success(Object obj) {
        return success(obj, null);
    }

    public static Map<String, Object> success(Object obj, String msg) {
        Map<String, Object> response = new LinkedHashMap<>(2);
        response.put("success",ResponseConstant.TRUE);
        response.put("code", ResponseConstant.SUCCESS);
        response.put("message", msg);
        response.put("result", obj);
        return response;
    }

    public static Map<String, Object> operationFailed(String msg) {
        Map<String, Object> response = new LinkedHashMap<>(2);
        response.put("success",ResponseConstant.FAIL);
        response.put("code", ResponseConstant.OPERATION_FAIL);
        response.put("message", msg);
        return response;
    }

    public static Map<String, Object> checkFailed(String msg) {
        Map<String, Object> response = new LinkedHashMap<>(2);
        response.put("success",ResponseConstant.FAIL);
        response.put("code", ResponseConstant.CHECK_FAIL);
        response.put("message", msg);
        return response;
    }

    public static Map<String, Object> failData(Object data, String msg) {
        Map<String, Object> response = new LinkedHashMap<>(3);
        response.put("success",ResponseConstant.FAIL);
        response.put("code", ResponseConstant.FAIL_DATA);
        response.put("result", data);
        response.put("message", msg);
        return response;
    }

    public static Map<String, Object> authFailed() {
        Map<String, Object> response = new LinkedHashMap<>(2);
        response.put("success",ResponseConstant.FAIL);
        response.put("code", ResponseConstant.AUTHENTICATION_FAILED);
        response.put("message", ResponseConstant.AUTHENTICATION_FAILED_STR);
        return response;
    }

    public static Map<String, Object> requestFailed(String message) {
        Map<String, Object> response = new LinkedHashMap<>(2);
        response.put("success",ResponseConstant.FAIL);
        response.put("code", ResponseConstant.ERROR);
        response.put("message", message);
        response.put("responseTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss.SSS")));
        return response;
    }

    public static Map<String, Object> operationPermissionDenied() {
        Map<String, Object> response = new LinkedHashMap<>(2);
        response.put("code", ResponseConstant.OPERATION_PERMISSION_DENIED);
        response.put("message", ResponseConstant.OPERATION_PERMISSION_DENIED_STR);
        return response;
    }

    public static Map<String, Object> operationPermissionDenied(String message) {
        Map<String, Object> response = new LinkedHashMap<>(2);
        response.put("code", ResponseConstant.OPERATION_PERMISSION_DENIED);
        response.put("message", message);
        return response;
    }

    public static Map<String, Object> dataPermissionDenied() {
        Map<String, Object> response = new LinkedHashMap<>(2);
        response.put("code", ResponseConstant.DATA_PERMISSION_DENIED);
        response.put("message", ResponseConstant.DATA_PERMISSION_DENIED_STR);
        return response;
    }

    public static Map<String, Object> dataPermissionDenied(String msg) {
        Map<String, Object> response = new LinkedHashMap<>(2);
        response.put("code", ResponseConstant.DATA_PERMISSION_DENIED);
        response.put("message", msg);
        return response;
    }

}
